package diamonds;

import org.apache.axis.client.Service;
import org.apache.axis.client.Call;

import javax.xml.namespace.QName;

public class LoginBean {
    private UserBean user;

    public void setUser(UserBean user) {
        this.user = user;
    }

    public String doLogin() {
        try {
            String endpoint =
                    "http://www.ebi.ac.uk:80/microarray-srv/EP/cgi-bin/epws_soap.pl";

            Service service = new Service();
            Call call = (Call)service.createCall();

            call.setTargetEndpointAddress(new java.net.URL(endpoint));
            call.setOperationName(new QName("http://www.ebi.ac.uk/EP/WS/SOAP", "epLogin"));
            call.addParameter("username", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
            call.addParameter("password", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
            call.setReturnType(org.apache.axis.Constants.XSD_STRING);

            String sessionId = (String) call.invoke(new Object[]{user.getName(), user.getPassword()});

            System.err.println("Sent '" + user.getName() + "/" + user.getPassword() + "', got '" + sessionId + "'");
            user.setSessionId(sessionId);
            return "success";

        } catch(java.rmi.RemoteException x) {
            System.err.println("Got SOAP error " + x.getMessage());
            user.setAuthError(x.getMessage());
            return "failure";
        }
        catch (Exception x) {
            System.err.println("Got exception");
            x.printStackTrace();
            return "failure";
        }
    }
}
